I have two dynamic drop down lists: State and City.
What I'm trying to do is first I need the city list to be empty on load. Then after a visitors selects the "state" from the first list, the page reloads and it shows all the available cities from my database where state is selected.
Here is what I have sofar:
Expand|Select|Wrap|Line Numbers
- <?php require_once('Connections/myConnection.php'); ?>
- <?php
- // Check for errors
- error_reporting(E_ALL);
- ini_set('display_errors', True);
- //
- if (!function_exists("GetSQLValueString")) {
- function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
- {
- $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
- $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
- switch ($theType) {
- case "text":
- $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
- break;
- case "long":
- case "int":
- $theValue = ($theValue != "") ? intval($theValue) : "NULL";
- break;
- case "double":
- $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
- break;
- case "date":
- $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
- break;
- case "defined":
- $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
- break;
- }
- return $theValue;
- }
- }
- mysql_select_db($database_myConnection, $myConnection);
- $query_state_recordset = "SELECT DISTINCT `State` FROM programs ORDER BY `State` ASC";
- $state_recordset = mysql_query($query_state_recordset, $myConnection) or die(mysql_error());
- $row_state_recordset = mysql_fetch_assoc($state_recordset);
- $totalRows_state_recordset = mysql_num_rows($state_recordset);
- mysql_select_db($database_myConnection, $myConnection);
- $query_city_recordset = "SELECT DISTINCT City FROM programs ORDER BY City ASC";
- $city_recordset = mysql_query($query_city_recordset, $myConnection) or die(mysql_error());
- $row_city_recordset = mysql_fetch_assoc($city_recordset);
- $totalRows_city_recordset = mysql_num_rows($city_recordset);
- ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Untitled Document</title>
- </head>
- <body>
- <form id="form1" name="form1" method="post" action="">
- <label></label>
- <p>
- <label>State
- <select name="state_id" id="state_id">
- <option value=""></option>
- <?php
- do {
- ?>
- <option value="<?php echo $row_state_recordset['State']?>"><?php echo $row_state_recordset['State']?></option>
- <?php
- } while ($row_state_recordset = mysql_fetch_assoc($state_recordset));
- $rows = mysql_num_rows($state_recordset);
- if($rows > 0) {
- mysql_data_seek($state_recordset, 0);
- $row_state_recordset = mysql_fetch_assoc($state_recordset);
- }
- ?>
- </select>
- </label>
- </p>
- <p>
- <label>City
- <select name="city_ig" id="city_ig">
- <option value=""></option>
- <?php
- do {
- ?>
- <option value="<?php echo $row_city_recordset['City']?>"><?php echo $row_city_recordset['City']?></option>
- <?php
- } while ($row_city_recordset = mysql_fetch_assoc($city_recordset));
- $rows = mysql_num_rows($city_recordset);
- if($rows > 0) {
- mysql_data_seek($city_recordset, 0);
- $row_city_recordset = mysql_fetch_assoc($city_recordset);
- }
- ?>
- </select>
- </label>
- </p>
- </form>
- </body>
- </html>
- <?php
- mysql_free_result($state_recordset);
- mysql_free_result($city_recordset);
- ?>
Thanks